package com.xjtu.duansheli.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xjtu.duansheli.entity.IdentifyImagesInfo;
import com.xjtu.duansheli.entity.IdentifyInfo;
import com.xjtu.duansheli.exception.MyException;
import com.xjtu.duansheli.mapper.IdentifyImagesInfoMapper;
import com.xjtu.duansheli.mapper.IdentifyInfoMapper;
import com.xjtu.duansheli.service.IdentifyInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 漩涡鸣人
 * @since 2020-10-24
 */
@Service
public class IdentifyInfoServiceImpl extends ServiceImpl<IdentifyInfoMapper, IdentifyInfo> implements IdentifyInfoService {

    @Autowired
    private IdentifyInfoMapper identifyInfoMapper;

    @Autowired
    private IdentifyImagesInfoMapper identifyImagesInfoMapper;


    @Override
    public List<IdentifyInfo> getAllIdentifyInfo(int current, int size) {
        Page<IdentifyInfo> page = new Page<>(current,size);
        identifyInfoMapper.selectPage(page, null);
        System.out.println(page.getTotal());
        return page.getRecords();
//        List<IdentifyInfo> identifyInfos = identifyInfoMapper.selectList(null);
//        if (identifyInfos.size() == 0) {
//            throw new MyException(500, "数据库中无数据！");
//        }
//        return identifyInfos;
    }

    @Override
    public IdentifyInfo getIdentifyInfoByid(Long id) {
        IdentifyInfo identifyInfo = identifyInfoMapper.selectById(id);
        if (identifyInfo == null) {
            throw new MyException(500, "不存在该认证记录！");
        }
        return identifyInfo;
    }

    @Override
    public IdentifyInfo getIdentifyByImid(Long imid) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("image_id",imid);

        List<IdentifyInfo> identifyInfos = identifyInfoMapper.selectByMap(map);
        if(identifyInfos.size() == 0){
            throw new MyException(500,"该图片未进行审批！");
        }
        return identifyInfos.get(0);
    }

    @Override
    public int addIdentifyInfo(IdentifyInfo identifyInfo) {
        if (identifyInfo == null){
            throw new MyException(500,"未提交审批记录！");
        }
        int result = identifyInfo.getResult();
        if(result == 1){
            try{
                IdentifyImagesInfo identifyImagesInfo = new IdentifyImagesInfo();
                identifyImagesInfo.setMid(identifyInfo.getImageId());
                identifyImagesInfo.setIsIdentify(1);
                identifyImagesInfoMapper.updateById(identifyImagesInfo);
            }catch (Exception e){
                System.out.println(e.getMessage());
                throw new MyException(500,"追加图片记录认证结果字段失败！");
            }
        }
        try{
            return identifyInfoMapper.insert(identifyInfo);
        }catch (Exception e){
            System.out.println(e.getMessage());
            throw new MyException(500,"添加认证记录失败！");
        }
    }

    @Override
    public int updateIdentifyInfo(IdentifyInfo identifyInfo) {
        try{
            return identifyInfoMapper.updateById(identifyInfo);
        }catch (Exception e){
            System.out.println(e.getMessage());
            throw new MyException(500,"更新认证记录失败！");
        }
    }

    @Override
    public int deleteIdentifyInfo(Long id) {
        try{
            return identifyInfoMapper.deleteById(id);
        }catch (Exception e){
            System.out.println(e.getMessage());
            throw new MyException(500,"删除认证记录失败！");
        }
    }

    @Override
    public int getTotal() {
        return identifyInfoMapper.selectCount(null);
    }
}
